package com.ys.extend.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.ys.WebConstants;

/**
 * 上下文工具类
 * @author wdj
 *
 */
public class ContextHolderUtils {
	/**
	 * SpringMvc下获取request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;

	}
	/**
	 * SpringMvc下获取session
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;

	}
	
	public static Integer getSessionUserId(){
		Integer userId= getSession().getAttribute(WebConstants.SECURITY_SESSION_USERID_ATTR)==null?null:(Integer)getSession().getAttribute(WebConstants.SECURITY_SESSION_USERID_ATTR);
		return userId;
	}
	
	public static String getSessionUsername(){
		 String username = getSession().getAttribute(WebConstants.SECURITY_SESSION_USERNAME_ATTR)==null?null:(String)getSession().getAttribute(WebConstants.SECURITY_SESSION_USERNAME_ATTR);
		 return username;
	}
	
	
}
